{% extends "base.html" %}

{% block content %}
<div class="container py-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1 class="h3">用户管理</h1>
    </div>

    <!-- 搜索和筛选表单 -->
    <div class="card shadow-sm mb-4">
        <div class="card-body">
            <form method="get" class="row g-3">
                <div class="col-md-4">
                    <div class="input-group">
                        <input type="text" class="form-control" name="search" value="{{ search }}" 
                               placeholder="搜索用户名或邮箱...">
                        <button class="btn btn-primary" type="submit">
                            <i class="fas fa-search"></i> 搜索
                        </button>
                    </div>
                </div>
                <div class="col-md-2">
                    <select class="form-select" name="status">
                        <option value="" {% if not status %}selected{% endif %}>所有状态</option>
                        <option value="active" {% if status == 'active' %}selected{% endif %}>启用</option>
                        <option value="inactive" {% if status == 'inactive' %}selected{% endif %}>禁用</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <select class="form-select" name="role">
                        <option value="" {% if not role %}selected{% endif %}>所有角色</option>
                        <option value="admin" {% if role == 'admin' %}selected{% endif %}>管理员</option>
                        <option value="user" {% if role == 'user' %}selected{% endif %}>普通用户</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <select class="form-select" name="per_page">
                        <option value="10" {% if per_page == 10 %}selected{% endif %}>10条/页</option>
                        <option value="20" {% if per_page == 20 %}selected{% endif %}>20条/页</option>
                        <option value="50" {% if per_page == 50 %}selected{% endif %}>50条/页</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-secondary w-100">
                        <i class="fas fa-filter"></i> 应用筛选
                    </button>
                </div>
            </form>
        </div>
    </div>

    <div class="card shadow-sm">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>
                                <a href="{{ url_for('admin.users', sort='username', 
                                         order='asc' if sort == 'username' and order == 'desc' else 'desc',
                                         search=search, status=status, role=role, per_page=per_page) }}" 
                                   class="text-decoration-none text-dark">
                                    用户名
                                    {% if sort == 'username' %}
                                        <i class="fas fa-sort-{{ 'down' if order == 'desc' else 'up' }}"></i>
                                    {% endif %}
                                </a>
                            </th>
                            <th>
                                <a href="{{ url_for('admin.users', sort='email',
                                         order='asc' if sort == 'email' and order == 'desc' else 'desc',
                                         search=search, status=status, role=role, per_page=per_page) }}"
                                   class="text-decoration-none text-dark">
                                    邮箱
                                    {% if sort == 'email' %}
                                        <i class="fas fa-sort-{{ 'down' if order == 'desc' else 'up' }}"></i>
                                    {% endif %}
                                </a>
                            </th>
                            <th>
                                <a href="{{ url_for('admin.users', sort='created_at',
                                         order='asc' if sort == 'created_at' and order == 'desc' else 'desc',
                                         search=search, status=status, role=role, per_page=per_page) }}"
                                   class="text-decoration-none text-dark">
                                    注册时间
                                    {% if sort == 'created_at' %}
                                        <i class="fas fa-sort-{{ 'down' if order == 'desc' else 'up' }}"></i>
                                    {% endif %}
                                </a>
                            </th>
                            <th>状态</th>
                            <th>角色</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for user in users.items %}
                        <tr>
                            <td>{{ user.id }}</td>
                            <td>{{ user.username }}</td>
                            <td>{{ user.email }}</td>
                            <td>{{ user.created_at.strftime('%Y-%m-%d %H:%M') }}</td>
                            <td>
                                {% if user.is_active %}
                                <span class="badge bg-success">启用</span>
                                {% else %}
                                <span class="badge bg-danger">禁用</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if user.is_admin %}
                                <span class="badge bg-primary">管理员</span>
                                {% else %}
                                <span class="badge bg-secondary">普通用户</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if not user.is_admin or current_user.id == user.id %}
                                <form action="{{ url_for('admin.toggle_user_active', id=user.id) }}" 
                                      method="post" class="d-inline">
                                    {% for key, value in request.args.items() %}
                                    <input type="hidden" name="{{ key }}" value="{{ value }}">
                                    {% endfor %}
                                    <button type="submit" class="btn btn-sm btn-outline-warning">
                                        {% if user.is_active %}禁用{% else %}启用{% endif %}
                                    </button>
                                </form>
                                {% if not user.is_admin %}
                                <form action="{{ url_for('admin.delete_user', id=user.id) }}" 
                                      method="post" class="d-inline"
                                      onsubmit="return confirm('确定要删除此用户吗？此操作不可恢复！');">
                                    {% for key, value in request.args.items() %}
                                    <input type="hidden" name="{{ key }}" value="{{ value }}">
                                    {% endfor %}
                                    <button type="submit" class="btn btn-sm btn-outline-danger">删除</button>
                                </form>
                                {% endif %}
                                {% endif %}
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>

            <!-- 分页 -->
            {% if users.pages > 1 %}
            <nav aria-label="Page navigation" class="mt-4">
                <ul class="pagination justify-content-center">
                    <!-- 上一页 -->
                    <li class="page-item {% if not users.has_prev %}disabled{% endif %}">
                        <a class="page-link" href="{{ url_for('admin.users', page=users.prev_num, 
                           search=search, status=status, role=role, sort=sort, order=order, per_page=per_page) }}">
                            上一页
                        </a>
                    </li>

                    <!-- 第一页 -->
                    {% if users.page - 2 > 1 %}
                    <li class="page-item">
                        <a class="page-link" href="{{ url_for('admin.users', page=1,
                           search=search, status=status, role=role, sort=sort, order=order, per_page=per_page) }}">
                            1
                        </a>
                    </li>
                    {% if users.page - 2 > 2 %}
                    <li class="page-item disabled"><span class="page-link">...</span></li>
                    {% endif %}
                    {% endif %}

                    <!-- 页码 -->
                    {% for p in range(users.pages + 1) %}
                        {% if p > 0 and p > users.page - 3 and p < users.page + 3 %}
                        <li class="page-item {% if p == users.page %}active{% endif %}">
                            <a class="page-link" href="{{ url_for('admin.users', page=p,
                               search=search, status=status, role=role, sort=sort, order=order, per_page=per_page) }}">
                                {{ p }}
                            </a>
                        </li>
                        {% endif %}
                    {% endfor %}

                    <!-- 最后一页 -->
                    {% if users.page + 2 < users.pages %}
                    {% if users.page + 2 < users.pages - 1 %}
                    <li class="page-item disabled"><span class="page-link">...</span></li>
                    {% endif %}
                    <li class="page-item">
                        <a class="page-link" href="{{ url_for('admin.users', page=users.pages,
                           search=search, status=status, role=role, sort=sort, order=order, per_page=per_page) }}">
                            {{ users.pages }}
                        </a>
                    </li>
                    {% endif %}

                    <!-- 下一页 -->
                    <li class="page-item {% if not users.has_next %}disabled{% endif %}">
                        <a class="page-link" href="{{ url_for('admin.users', page=users.next_num,
                           search=search, status=status, role=role, sort=sort, order=order, per_page=per_page) }}">
                            下一页
                        </a>
                    </li>
                </ul>
            </nav>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %} 